package cn.bugstack.mybatis.reflection.invoker;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
 * @author wcc
 * @description 方法调用者
 * @create
 */

public class MethodInvoker implements Invoker {
    private Method method;
    private Class<?> type;

    public MethodInvoker(Method method) {
        this.method = method;
        //如果只有一个参数返回参数类型 否则返回return类型
        if (method.getParameterTypes().length == 1) {
            type = method.getParameterTypes()[0];
        } else {
            type = method.getReturnType();
        }
    }

    @Override
    public Object invoke(Object target, Object[] args) throws InvocationTargetException, IllegalAccessException {
        return method.invoke(target, args);
    }

    @Override
    public Class<?> getType() {
        return type;
    }
}
